package com.luocj.muke.demo.utils;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * Created by lxf on 2018/7/16.
 * Sp工具类
 */

public class SPUtils {
    private static SharedPreferences mSp;

    //test
    public static void putBoolean(Context context, String key, boolean value) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }
        mSp.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(Context context, String key, boolean defValue) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }
        return mSp.getBoolean(key, defValue);
    }

    public static void putString(Context context, String key, String value) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }
        mSp.edit().putString(key, value).commit();
    }

    public static String getString(Context context, String key, String defValue) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }
        return mSp.getString(key, defValue);
    }

    public static void putInt(Context context, String key, Integer integer) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }
        mSp.edit().putString(key, String.valueOf(integer)).commit();
    }

    public static String getInt(Context context, String key, Integer integer) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }
        return mSp.getString(key, String.valueOf(integer));
    }

    public static void putDouble(Context context, String key, Double value) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }
        mSp.edit().putString(key, String.valueOf(value)).commit();
    }

    public static String getDouble(Context context, String key, Double defValue) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }
        return mSp.getString(key, String.valueOf(defValue));
    }

    public static void removeBykey(Context context, String key) {
        if (mSp == null) {
            mSp = context.getSharedPreferences("app", Context.MODE_PRIVATE);
        }

        SharedPreferences.Editor editor = mSp.edit();
        editor.remove(key);
        editor.commit();
    }

}
